import { d as defineComponent, s as storeToRefs, k as onMounted, r as ref, l as reactive, o as openBlock, c as createElementBlock, f as createBaseVNode, b as createVNode, w as withCtx, m as withKeys, a as resolveComponent, h as unref, i as createTextVNode, t as toDisplayString, F as Fragment, e as renderList, E as ElMessage, n as search_default, g as createBlock, _ as _export_sfc } from "./index-NZguwuNX.js";
import { u as useCounterStore, h as http } from "./index-F6B3vg3q.js";
const _hoisted_1 = { class: "agent" };
const _hoisted_2 = { class: "df jc-sb title-btn" };
const _hoisted_3 = { style: { "margin-left": "5px" } };
const _hoisted_4 = { style: { "margin-left": "5px" } };
const _hoisted_5 = { class: "dialog-footer" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "juniorAgent",
  setup(__props) {
    const store = useCounterStore();
    const { edit_table, formart_sql, delete_sql } = store;
    const { userInfo } = storeToRefs(store);
    onMounted(async () => {
      await get_list("SELECT * FROM agentuser ORDER BY agentuser.AgentUserName LIMIT 15 OFFSET 0", "userglobal");
      console.log(tableData.value);
      console.log(Object.keys(tableData.value[0]));
    });
    const tableData = ref([]);
    const iphone_rules = (rule, value, callback) => {
      console.log(rule, value, callback);
      let reg = /^[1][3-9]\d{9}$/;
      if (value === "") {
        callback(new Error("请输入手机号"));
      } else if (reg.test(value)) {
        callback();
      } else {
        callback(new Error("请输入11位的手机号"));
      }
    };
    const rules = reactive({
      AgentUserName: [
        { required: true, message: "请输入用户名", trigger: "change" },
        { min: 3, max: 10, message: "长度3-10个字符", trigger: "change" }
      ],
      AgentUserPassword: [
        { required: true, message: "请输入密码", trigger: "change" },
        { min: 4, max: 18, message: "长度4-18位字符", trigger: "change" }
      ],
      Phone: [
        { validator: iphone_rules, trigger: "change", required: true }
        // { min: 11, max: 11, message: "长度11位数字", trigger: "blur" },
      ]
    });
    const formData = ref({
      AgentLevel: "",
      //层级
      AgentMisc: "",
      AgentNotice: "",
      AgentStatus: "",
      // 封禁状态
      AgentUserName: "",
      AgentUserPassword: "",
      IDCard: "",
      LoginIP: "",
      ManagerAgentUserName: null,
      //上级代理
      Phone: ""
    });
    const is_Status = ref(true);
    const dialogVisible = ref(false);
    ref("");
    const is_add_edit = ref("");
    const edit = async (row) => {
      console.log(row);
      is_add_edit.value = "edit";
      formData.value = row;
      is_Status.value = row.AgentStatus == "0" ? true : false;
      dialogVisible.value = true;
    };
    const enter = async () => {
      formData.value.AgentStatus = is_Status.value ? "0" : "1";
      formData.value.ManagerAgentUserName = formData.value.ManagerAgentUserName == "" ? null : formData.value.ManagerAgentUserName;
      formData.value.AgentLevel = formData.value.AgentLevel == "" ? "0" : formData.value.AgentLevel;
      delete formData.value.status;
      let sql_formart = await formart_sql(formData.value);
      console.log(sql_formart);
      if (is_add_edit.value === "edit") {
        let sql = `UPDATE agentuser SET ${sql_formart} WHERE AgentUserName = '${formData.value.AgentUserName}'; `;
        console.log(sql);
        const res = await edit_table(sql, "userglobal");
        dialogVisible.value = false;
        if (res && res.packageStatus === 0) {
          ElMessage({
            message: "修改成功",
            type: "success"
          });
          setTimeout(async () => {
            await get_list("SELECT * FROM agentuser", "userglobal");
          }, 100);
        } else {
          ElMessage({
            message: "修改失败",
            type: "error"
          });
        }
        console.log(tableData.value);
        console.log(res);
      } else {
        console.log(formData.value);
        let arr = Object.keys(formData.value);
        let key_string = "";
        let value_string = "";
        for (var i = 0; i < arr.length; i++) {
          if (i == arr.length - 1) {
            key_string += arr[i] + "";
            if (formData.value[arr[i]] == null) {
              value_string += `${formData.value[arr[i]]}`;
            } else {
              value_string += `'${formData.value[arr[i]]}'`;
            }
          } else {
            key_string += arr[i] + ",";
            if (formData.value[arr[i]] == null) {
              value_string += `${formData.value[arr[i]]},`;
            } else {
              value_string += `'${formData.value[arr[i]]}',`;
            }
          }
        }
        console.log(key_string);
        console.log(value_string);
        let sql = `INSERT INTO agentuser (${key_string}) VALUES(${value_string})`;
        const res = await edit_table(sql, "userglobal");
        console.log(res);
        dialogVisible.value = false;
        if (res && res.packageStatus === 0) {
          ElMessage({
            message: "添加成功",
            type: "success"
          });
          setTimeout(async () => {
            await get_list("SELECT * FROM agentuser", "userglobal");
          }, 100);
        } else {
          ElMessage({
            message: "添加失败",
            type: "error"
          });
        }
      }
    };
    const switch_change = async (row) => {
      console.log(row);
      formData.value = row;
      is_Status.value = row.status;
      is_add_edit.value = "edit";
      console.log(is_Status.value);
      await enter();
    };
    const insert_into = () => {
      dialogVisible.value = true;
      is_add_edit.value = "add";
      formData.value = {
        AgentLevel: "",
        //层级
        AgentMisc: "",
        AgentNotice: "",
        AgentStatus: "",
        // 封禁状态
        AgentUserName: "",
        AgentUserPassword: "",
        IDCard: "",
        LoginIP: "",
        ManagerAgentUserName: "",
        //上级代理
        Phone: ""
      };
    };
    const prev_list = ref([]);
    const get_list = async (sql, dbName) => {
      console.log(userInfo.value);
      const res = await http.corsApi("/", "post", {
        packID: "123456",
        packageType: "adminSql",
        packageContent: {
          AgentUserName: userInfo.value.username,
          AgentUserPassword: userInfo.value.password,
          sql,
          dbName
        }
      });
      let result_adta = [];
      if (res.packageContent.packageMsg == "成功") {
        result_adta = [];
      } else {
        let data = JSON.parse(res.packageContent.packageMsg);
        result_adta = data;
        data.map((item) => {
          item["status"] = item.AgentStatus == 1 ? false : true;
        });
        console.log(del_copy(data));
        prev_list.value = [
          ...del_copy(data),
          {
            AgentUserName: "最高管理",
            AgentLevel: "-1",
            ManagerAgentUserName: null,
            AgentMisc: "",
            AgentNotice: "",
            AgentStatus: "",
            // 封禁状态
            AgentUserPassword: "",
            IDCard: "",
            LoginIP: "",
            Phone: ""
          }
        ];
        console.log(tableData.value);
      }
      tableData.value = result_adta;
      return result_adta;
    };
    const del_copy = (arr) => {
      let list = [];
      return arr.filter((item) => !list.includes(item.AgentUserName) && list.push(item.AgentUserName));
    };
    const select_change = (item) => {
      console.log(item);
      console.log(item.AgentLevel * 1 + 1 + "");
      formData.value.AgentLevel = item.AgentLevel * 1 + 1 + "";
    };
    const enter_del = async (row) => {
      console.log(row);
      const res = await delete_sql(`DELETE FROM agentuser WHERE AgentUserName = '${row.AgentUserName}'`, "userglobal");
      console.log(res);
      if (res && res.packageStatus === 0) {
        ElMessage({
          message: "删除成功",
          type: "success"
        });
        setTimeout(async () => {
          await get_list("SELECT * FROM agentuser", "userglobal");
        }, 100);
      } else {
        ElMessage({
          message: "删除失败",
          type: "error"
        });
      }
    };
    const search_inp = ref("");
    const search_click = async () => {
      if (search_inp.value == "") {
        await get_list("SELECT * FROM agentuser ORDER BY agentuser.AgentUserName LIMIT 15 OFFSET 0", "userglobal");
      }
      await get_list(
        `SELECT * FROM agentuser WHERE AgentUserName LIKE '%${search_inp.value}%' OR Phone LIKE  '%${search_inp.value}%' OR LoginIP LIKE  '%${search_inp.value}%' OR IDCard LIKE  '%${search_inp.value}%' OR AgentNotice LIKE  '%${search_inp.value}%'`,
        "userglobal"
      );
    };
    return (_ctx, _cache) => {
      const _component_el_button = resolveComponent("el-button");
      const _component_el_input = resolveComponent("el-input");
      const _component_el_table_column = resolveComponent("el-table-column");
      const _component_el_switch = resolveComponent("el-switch");
      const _component_el_popconfirm = resolveComponent("el-popconfirm");
      const _component_el_table = resolveComponent("el-table");
      const _component_el_option = resolveComponent("el-option");
      const _component_el_select = resolveComponent("el-select");
      const _component_el_form_item = resolveComponent("el-form-item");
      const _component_el_form = resolveComponent("el-form");
      const _component_el_dialog = resolveComponent("el-dialog");
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createBaseVNode("div", _hoisted_2, [
          createBaseVNode("div", null, [
            createVNode(_component_el_input, {
              modelValue: search_inp.value,
              "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => search_inp.value = $event),
              onKeydown: withKeys(search_click, ["enter"]),
              placeholder: "输入任意字段进行模糊查询",
              class: "input-with-select"
            }, {
              append: withCtx(() => [
                createVNode(_component_el_button, {
                  onClick: search_click,
                  icon: unref(search_default)
                }, null, 8, ["icon"])
              ]),
              _: 1
            }, 8, ["modelValue"])
          ]),
          createVNode(_component_el_button, {
            type: "primary",
            onClick: insert_into
          }, {
            default: withCtx(() => [
              createTextVNode("代理开户")
            ]),
            _: 1
          })
        ]),
        createVNode(_component_el_table, {
          data: tableData.value,
          style: { "width": "100%", "margin-top": "15px" },
          border: ""
        }, {
          default: withCtx(() => [
            createVNode(_component_el_table_column, {
              prop: "ManagerAgentUserName",
              label: "上级代理"
            }),
            createVNode(_component_el_table_column, {
              prop: "AgentLevel",
              label: "代理层级"
            }, {
              default: withCtx((scope) => [
                createBaseVNode("span", null, toDisplayString(scope.row.AgentLevel == 0 ? "最高代理" : scope.row.AgentLevel == 1 ? "一级代理" : scope.row.AgentLevel == 2 ? "二级代理" : scope.row.AgentLevel == 3 ? "三级代理" : `${scope.row.AgentLevel}级代理`), 1)
              ]),
              _: 1
            }),
            createVNode(_component_el_table_column, {
              prop: "AgentUserName",
              label: "用户名"
            }),
            createVNode(_component_el_table_column, {
              prop: "AgentUserPassword",
              label: "密码"
            }),
            createVNode(_component_el_table_column, {
              prop: "Phone",
              label: "手机号"
            }),
            createVNode(_component_el_table_column, {
              prop: "LoginIP",
              label: "登录IP"
            }),
            createVNode(_component_el_table_column, {
              prop: "status",
              label: "状态"
            }, {
              default: withCtx((scope) => [
                createBaseVNode("div", null, [
                  createVNode(_component_el_switch, {
                    modelValue: scope.row.status,
                    "onUpdate:modelValue": ($event) => scope.row.status = $event,
                    onChange: ($event) => switch_change(scope.row)
                  }, null, 8, ["modelValue", "onUpdate:modelValue", "onChange"]),
                  createBaseVNode("span", _hoisted_3, toDisplayString(scope.row.status ? "开启" : "关闭"), 1)
                ])
              ]),
              _: 1
            }),
            createVNode(_component_el_table_column, {
              prop: "",
              label: "操作",
              width: "300"
            }, {
              default: withCtx((scope) => [
                createVNode(_component_el_button, {
                  onClick: ($event) => edit(scope.row)
                }, {
                  default: withCtx(() => [
                    createTextVNode("修改")
                  ]),
                  _: 2
                }, 1032, ["onClick"]),
                createVNode(_component_el_popconfirm, {
                  title: "不在考虑一下?",
                  "confirm-button-text": "确定",
                  "cancel-button-text": "取消",
                  onConfirm: ($event) => enter_del(scope.row)
                }, {
                  reference: withCtx(() => [
                    createVNode(_component_el_button, null, {
                      default: withCtx(() => [
                        createTextVNode("删除")
                      ]),
                      _: 1
                    })
                  ]),
                  _: 2
                }, 1032, ["onConfirm"])
              ]),
              _: 1
            })
          ]),
          _: 1
        }, 8, ["data"]),
        createVNode(_component_el_dialog, {
          draggable: "",
          modelValue: dialogVisible.value,
          "onUpdate:modelValue": _cache[8] || (_cache[8] = ($event) => dialogVisible.value = $event),
          title: "提示",
          width: "40%"
        }, {
          footer: withCtx(() => [
            createBaseVNode("span", _hoisted_5, [
              createVNode(_component_el_button, {
                onClick: _cache[7] || (_cache[7] = ($event) => dialogVisible.value = false)
              }, {
                default: withCtx(() => [
                  createTextVNode("取消")
                ]),
                _: 1
              }),
              createVNode(_component_el_button, {
                type: "primary",
                onClick: enter
              }, {
                default: withCtx(() => [
                  createTextVNode(" 确认 ")
                ]),
                _: 1
              })
            ])
          ]),
          default: withCtx(() => [
            createVNode(_component_el_form, {
              style: { "padding-right": "40px" },
              ref: "ruleFormRef",
              model: formData.value,
              rules,
              "label-width": "120px",
              class: "demo-ruleForm",
              "status-icon": ""
            }, {
              default: withCtx(() => [
                createVNode(_component_el_form_item, {
                  label: "上级代理:",
                  prop: "ManagerAgentUserName"
                }, {
                  default: withCtx(() => [
                    createVNode(_component_el_select, {
                      modelValue: formData.value.ManagerAgentUserName,
                      "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => formData.value.ManagerAgentUserName = $event),
                      placeholder: "请选择上级代理",
                      style: { "width": "100%" }
                    }, {
                      default: withCtx(() => [
                        (openBlock(true), createElementBlock(Fragment, null, renderList(prev_list.value, (item, index) => {
                          return openBlock(), createBlock(_component_el_option, {
                            key: index,
                            label: item.AgentUserName,
                            value: item.AgentUserName,
                            onClick: ($event) => select_change(item)
                          }, null, 8, ["label", "value", "onClick"]);
                        }), 128))
                      ]),
                      _: 1
                    }, 8, ["modelValue"])
                  ]),
                  _: 1
                }),
                createVNode(_component_el_form_item, {
                  label: "用户名:",
                  prop: "AgentUserName"
                }, {
                  default: withCtx(() => [
                    createVNode(_component_el_input, {
                      modelValue: formData.value.AgentUserName,
                      "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => formData.value.AgentUserName = $event)
                    }, null, 8, ["modelValue"])
                  ]),
                  _: 1
                }),
                createVNode(_component_el_form_item, {
                  label: "密码:",
                  prop: "AgentUserPassword"
                }, {
                  default: withCtx(() => [
                    createVNode(_component_el_input, {
                      modelValue: formData.value.AgentUserPassword,
                      "onUpdate:modelValue": _cache[3] || (_cache[3] = ($event) => formData.value.AgentUserPassword = $event)
                    }, null, 8, ["modelValue"])
                  ]),
                  _: 1
                }),
                createVNode(_component_el_form_item, {
                  label: "手机号:",
                  prop: "Phone"
                }, {
                  default: withCtx(() => [
                    createVNode(_component_el_input, {
                      modelValue: formData.value.Phone,
                      "onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => formData.value.Phone = $event)
                    }, null, 8, ["modelValue"])
                  ]),
                  _: 1
                }),
                createVNode(_component_el_form_item, {
                  label: "登录IP:",
                  prop: "IP"
                }, {
                  default: withCtx(() => [
                    createVNode(_component_el_input, {
                      modelValue: formData.value.LoginIP,
                      "onUpdate:modelValue": _cache[5] || (_cache[5] = ($event) => formData.value.LoginIP = $event)
                    }, null, 8, ["modelValue"])
                  ]),
                  _: 1
                }),
                createVNode(_component_el_form_item, {
                  label: "状态:",
                  prop: "AgentStatus"
                }, {
                  default: withCtx(() => [
                    createVNode(_component_el_switch, {
                      modelValue: is_Status.value,
                      "onUpdate:modelValue": _cache[6] || (_cache[6] = ($event) => is_Status.value = $event)
                    }, null, 8, ["modelValue"]),
                    createBaseVNode("span", _hoisted_4, toDisplayString(is_Status.value ? "开启" : "关闭"), 1)
                  ]),
                  _: 1
                })
              ]),
              _: 1
            }, 8, ["model", "rules"])
          ]),
          _: 1
        }, 8, ["modelValue"])
      ]);
    };
  }
});
const juniorAgent = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-f77784ca"]]);
export {
  juniorAgent as default
};
